package com.zky.org.service.impl;

import com.zky.basic.exception.BusinessException;
import com.zky.org.domain.Employee;
import com.zky.org.domain.Shop;
import com.zky.org.domain.ShopAuditLog;
import com.zky.org.mapper.EmployeeMapper;
import com.zky.org.mapper.ShopAuditLogMapper;
import com.zky.org.mapper.ShopMapper;
import com.zky.org.service.IShopService;
import com.zky.basic.service.impl.BaseServiceImpl;

import com.zky.user.domain.Logininfo;
import com.zky.user.mapper.LogininfoMapper;
import com.zky.util.BaiduAiUtils;
import com.zky.util.Md5Utils;
import com.zky.util.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {
        //一：检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        if(StringUtils.isEmpty(shop.getName())){ //空和空字符串返回true
            throw new BusinessException("提交资料不能为空!!!");
        }

        //校验店铺名称
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法!!!");
        }
        //校验logo
        if(!StringUtils.isEmpty(shop.getLogo())){
            if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw new BusinessException("logo不合法!!!");
            }
        }

        //二：校验 - 当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop.getName(),shop.getAddress());
        if(dbShop != null){//查到了 = 入驻过
            throw new BusinessException("请勿重复入驻，忘记密码请联系管理员!!!");
        }

        //添加logininfo信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.add(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.add(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShop_id(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);
    }



    @Override
    public void AuditPass(ShopAuditLog log) throws MessagingException {

        //1.改状态：
        Shop shop = shopMapper.findOne(log.getShopId());
        shopMapper.updateState(log.getShopId(),4);
        //2.添加 审核日志
        log.setState(0);
        log.setAuditId(350L);
        shopAuditLogMapper.add(log);
        //3.发送邮件
        //3.发邮件

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2315279694@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findOne(shop.getAdminId());
        helper.setTo("2315279694@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void active(Long id) {

        shopMapper.updateState(id,3);
    }

    @Override
    public void AuditReject(ShopAuditLog log) throws MessagingException {
        //1.改状态：
        Shop shop = shopMapper.findOne(log.getShopId());
        shopMapper.updateState(log.getShopId(),2);
        //2.添加 审核日志
        log.setState(1);
        log.setAuditId(350L);
        shopAuditLogMapper.add(log);
        //3.发送邮件
        //gcpqyjmysftediic
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2315279694@qq.com");
        helper.setSubject("店铺激活失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://127.0.0.1:8081/#/regiseterEdit/"+log.getShopId()
                +"'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findOne(shop.getAdminId());
        helper.setTo("2315279694@qq.com");
        //发送
        javaMailSender.send(mimeMessage);


    }
}
